/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */
package com.fuyao.cloud.admin.flow.service.flowable.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuyao.cloud.admin.flow.actor.ActorTypeFactory;
import com.fuyao.cloud.admin.flow.actor.HandlerContext;
import com.fuyao.cloud.admin.flow.service.flowable.ActorUserService;
import com.fuyao.cloud.admin.mapper.ActorUserMapper;
import com.fuyao.cloud.common.security.service.CloudUser;
import com.fuyao.cloud.common.security.util.SecurityUtils;
import com.fuyao.cloud.flowable.entity.ActorUser;
import com.fuyao.cloud.flowable.vo.ActorInstance;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 节点加载用户配置表
 *
 * @author liyue
 * @date 2022-08-10 11:10:11
 */
@Service
public class ActorUserServiceImpl extends ServiceImpl<ActorUserMapper, ActorUser> implements ActorUserService {

    @Resource
    private HandlerContext handlerContext;

    @Override
    public List<ActorInstance> getActorInstances(List<String> actorUserIds, String procInstId) {
        List<ActorUser> actorUserList = this.listByIds(actorUserIds);
        CloudUser cloudUser = SecurityUtils.getUser();
        List<ActorInstance> actorInstanceList = Lists.newArrayList();
        actorUserList.forEach(actorUser -> {
            ActorTypeFactory instance = handlerContext.getInstance(actorUser.getActorType());
            List<ActorInstance> actorInstances = instance.execute(cloudUser, actorUser, procInstId);
            actorInstanceList.addAll(actorInstances);
        });
        return actorInstanceList.stream().distinct().collect(Collectors.toList());
    }
}
